<!DOCTYPE html>
<html>
<head>
	<link href="styles.css" rel="stylesheet" type="text/css">
	<title>New Deposit</title>
</head>
<body>
	<?php
		// include of required files
		require_once ("constants.php");

		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
			$view = $_POST['planet_view'];
			$planet_id = $_POST['planet_id'];
			$planet_name = $_POST['planet_name'];
			$planet_size = $_POST['planet_size'];
			if(isset($_POST['x_coord'])) {$x_coord = $_POST['x_coord'];}
			if(isset($_POST['y_coord'])) {$y_coord = $_POST['y_coord'];}
		}
								
		// html header
		echo "<h1 align=\"center\">New Deposit</h1>";
		echo "<h2 align=\"center\">Planet Name: ".$planet_name."</h2>\n";

		/* Creates an insert button/form. If the working view is "map", redirects fo mapDepos.php; if not redirects
		 * to listDepos.php
		 */
		if($view === "map") {
			echo "<FORM METHOD=\"post\" ACTION=\"mapDepos.php\">";
		}
		else {
			echo "<FORM METHOD=\"post\" ACTION=\"listDepos.php\">";
		}
		
		echo "<input type=\"hidden\" name=\"db_action\" value=\"insert\">";
		echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">";

		$listCoord = "";
		
		// if we have x,y coords set by input, the x_coord y_coord input fields will be disabled and fixed to the input values
		if(isset($x_coord) && isset($y_coord)){
			
			echo"<p>X Coord <input type=\"text\" name=\"x_disp\" size=\"10\" value=".$x_coord." disabled></p>";
			echo"<input type=\"hidden\" name=\"x_coord\" value=".$x_coord." >";
			echo"<p>Y Coord <input type=\"text\" name=\"y_disp\" size=\"10\" value=".$y_coord." disabled></p>";
			echo"<input type=\"hidden\" name=\"y_coord\" value=".$y_coord." >";
			
		} else
		{
			echo "<p>X Coord <select name=\"x_coord\" size=\"1\">";
			
			for($i=0;$i<$planet_size;$i++){
				$listCoord .= "<option>$i</option>\n";
			}
			echo "$listCoord";
			echo "</select></p>\n";
			echo "<p>Y Coord <select name=\"y_coord\" size=\"1\">";
			echo "$listCoord";
			echo "</select></p>\n";
		}
						
	?>
	
	<p>Material <select name="material" size="1">
	<?php
		// create the materials dropbox from the values in $rm_array (set in constants.php)
		foreach($rm_array as $mat){
			echo "<option>".$mat."</option>\n";
		}
	?>
	</select></p>
	<p>Size <input type="text" name="dep_size" size="30"></p>
	<p><input type="submit" value="Create Deposit" name="enviar"></p>
	</FORM><br>

	<?php

		// "back to view" button
		if($view === "map") {
			
			echo "<FORM class=\"bar\" METHOD=\"post\" ACTION=\"mapDepos.php\">\n";
			echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">\n";
			echo "<input type=\"submit\" value=\"Back to Map View\" name=\"editar\">\n";
			echo "</FORM><br />\n";
		}
		else {
			
			echo "<FORM class=\"bar\" METHOD=\"post\" ACTION=\"listDepos.php\">\n";
			echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">\n";
			echo "<input type=\"submit\" value=\"Back to List View\" name=\"editar\">\n";
			echo "</FORM>\n";	
		}
	?>

	<br>
	<p align="center"><a href="index.php">Back to List of Planets</a></p>
</body>
</html>
